package com.dwy2002.pojo.po.refund;


import com.dwy2002.pojo.po.base.BaseDeleted;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * 退款记录表实体类
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/06
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class RefundRecords extends BaseDeleted {
    /**
     * 关联订单ID
     */
    private String orderId;
    /**
     * 退款订单类型：1-商品，2-课程，3-包间
     */
    private Integer refundOrderType;
    /**
     * 原支付记录ID
     */
    private String paymentRecordId;
    /**
     * 系统生成退款单号
     */
    private String refundNo;
    /**
     * 实际退款金额
     */
    @JsonSerialize(using = ToStringSerializer.class) // 将 BigDecimal 序列化为字符串
    private BigDecimal refundAmount;
    /**
     * 申请退款金额
     */
    @JsonSerialize(using = ToStringSerializer.class) // 将 BigDecimal 序列化为字符串
    private BigDecimal applyAmount;
    /**
     * 退款原因
     */
    private String refundReason;
    /**
     * 退款方式：1-原路退回 2-现金退款
     */
    private Integer refundMethod;
    /**
     * 状态：0-待处理 1-处理中 2-成功 3-失败
     */
    private Integer refundStatus;
    /**
     * 操作人ID（管理员）
     */
    private String operatorId;
    /**
     * 操作人类型：1-系统 2-管理员
     */
    private Integer operatorType;
    /**
     * 第三方退款流水号
     */
    private String thirdpartyRefundId;
    /**
     * 失败原因
     */
    private String failReason;

    @Getter
    public enum OperatorTypeEnum {
        SYSTEM(1, "系统自动操作"),
        ADMIN(2, "后台管理员");

        private final int code;
        private final String description;

        OperatorTypeEnum(int code, String description) {
            this.code = code;
            this.description = description;
        }

        /**
         * 校验方法
         *
         * @param code 数值
         * @return true：合法 false：不合法
         * @author 杜伟毅
         * @since 2025/3/15 0015 22:12
         */
        public static boolean isValidOperatorTypeEnum(int code) {
            for (OperatorTypeEnum o : OperatorTypeEnum.values()) {
                if (code == o.code) {
                    return true;
                }
            }
            return false;
        }
    }

    // 退款方式枚举
    @Getter
    public enum RefundMethodEnum {
        ORIGINAL_PATH(1, "原路退回"),
        CASH_REFUND(2, "现金退款");

        private final int code;
        private final String description;

        RefundMethodEnum(int code, String description) {
            this.code = code;
            this.description = description;
        }

        /**
         * 校验方法
         *
         * @param code 数值
         * @return true：合法 false：不合法
         * @author 杜伟毅
         * @since 2025/3/15 0015 22:12
         */
        public static boolean isValidRefundMethodEnum(int code) {
            for (RefundMethodEnum refundMethodEnum : RefundMethodEnum.values()) {
                if (code == refundMethodEnum.code) {
                    return true;
                }
            }
            return false;
        }
    }

    // 退款状态枚举
    @Getter
    public enum RefundStatusEnum {
        PENDING(0, "待处理"),
        PROCESSING(1, "处理中"),
        SUCCESS(2, "成功"),
        FAILED(3, "失败");

        private final int code;
        private final String description;

        RefundStatusEnum(int code, String description) {
            this.code = code;
            this.description = description;
        }

        /**
         * 校验方法
         *
         * @param code 数值
         * @return true：合法 false：不合法
         * @author 杜伟毅
         * @since 2025/3/15 0015 22:12
         */
        public static boolean isValidRefundMethodEnum(int code) {
            for (RefundStatusEnum r : RefundStatusEnum.values()) {
                if (code == r.code) {
                    return true;
                }
            }
            return false;
        }
    }

    @Getter
    public enum RefundOrderTypeEnum {
        PRODUCT(1, "productOrderHandler"),
        COURSE(2, "courseOrderHandler"),
        ROOM(3, "roomOrderHandler");

        private final int code;
        private final String handlerName;

        RefundOrderTypeEnum(int code, String handlerName) {
            this.code = code;
            this.handlerName = handlerName;
        }

        /**
         * 根据订单类型返回实现类
         *
         * @param code 订单类型
         * @return 1： 2： 3：
         * @author 杜伟毅
         * @since 2025/3/11 0011 23:11
         */
        public static String getHandlerNameByCode(int code) {
            return Arrays.stream(values())
                    .filter(type -> type.code == code)
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("无效的订单类型"))
                    .handlerName;
        }

        /**
         * 检测是否是合法订单类型
         *
         * @param value 订单类型数字
         * @return true:合法 false：不合法
         * @author 杜伟毅
         * @since 2025/3/11 0011 23:15
         */
        public static boolean isValidOrderTypeEnum(int value) {
            for (RefundOrderTypeEnum r : RefundOrderTypeEnum.values()) {
                if (value == r.code) {
                    return true;
                }
            }
            return false;
        }
    }
}
